package com.zishi.venuesbooking.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zishi.venuesbooking.R;
import com.zishi.venuesbooking.entity.Comment;
import com.zishi.venuesbooking.entity.Reserve;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import de.hdodenhof.circleimageview.CircleImageView;

public class ReserveAdapter extends RecyclerView.Adapter<ReserveAdapter.ReserveViewHolder> {

    private List<Reserve> reserves;

    public ReserveAdapter(List<Reserve> reserves) {
        this.reserves = reserves;
    }

    @Override
    public ReserveViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_my_reserve, parent, false);
        return new ReserveViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ReserveViewHolder holder, int position) {
        Reserve reserve = reserves.get(position);
        holder.bind(reserve);
    }

    @Override
    public int getItemCount() {
        return reserves.size();
    }

    // 更新数据
    public void updateData(List<Reserve> newReserves) {
        reserves.clear();
        reserves.addAll(newReserves);
        notifyDataSetChanged();
    }

    // 添加新评论
    public void addReserve(Reserve reserve) {
        reserves.add(0, reserve); // 添加到列表顶部
        notifyItemInserted(0);
    }

    static class ReserveViewHolder extends RecyclerView.ViewHolder {
        private final CircleImageView civAvatar = itemView.findViewById(R.id.civ_venue_avatar);
        private final TextView tvVenueName = itemView.findViewById(R.id.venue_name);
        private final TextView tvPrice = itemView.findViewById(R.id.price);

        public ReserveViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void bind(Reserve reserve) {
            // 加载用户头像
            Glide.with(itemView.getContext())
                    .load(reserve.getVenueAvatar())
                    .placeholder(R.drawable.avata_1)
                    .into(civAvatar);

            tvVenueName.setText(reserve.getVenueName());
            tvPrice.setText("$" + reserve.getPrice());
        }

        private String formatTime(long time) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            return sdf.format(new Date(time));
        }
    }
}
